Skip to content

Note about Dispose affecting Attachments #2902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2019
Merged

Note about Dispose affecting Attachments #2902

merged 1 commit into from
Aug 2, 2019

Conversation

dpmm99
Copy link

@dpmm99 dpmm99 commented Aug 1, 2019

Summary

Disposing of Attachments also closes the Streams they're using.

https://referencesource.microsoft.com/#system/net/System/Net/mail/MailMessage.cs,a22b7e48cd4bc831

The problem I had that caused me to dig into the code wasn't relevant to this change, but I saw it as a potential occasional issue. A MailMessage intuitively shouldn't take this kind of ownership of an Attachment that was given to it by an external source since the documentation suggests that Attachments can be reused. In my mind, "not intuitive" means "needs to be clearly documented."

(For the sake of discussion: the issue I had that made me dig into the code, while not directly related, was that reusing my MemoryStream for a second Attachment in a second MailMessage resulted in an email being sent with a 0-byte attachment. I don't know why MimePart.ResetStream() didn't reset the stream as I expected, but I'd say it was because the code spun off a thread to send each email, clearly having the potential for concurrency problems. I've since put my SmtpClient.Send call in a critical section, but knowing exactly what needed to be in a critical section also required digging into the code to see where exactly the stream was being used, and that should perhaps be documented somehow.)

https://referencesource.microsoft.com/#system/net/System/Net/mail/MailMessage.cs,a22b7e48cd4bc831
Disposing of Attachments also closes the Streams they're using.
The problem I had that caused me to dig into the code wasn't relevant to this change, but I saw it as a potential occasional issue. A MailMessage intuitively shouldn't take this kind of ownership of an Attachment that was given to it by an external source since the documentation suggests that Attachments can be reused. In my mind, "not intuitive" means "needs to be clearly documented."
@dpmm99 dpmm99 requested a review from karelz as a code owner August 1, 2019 15:04
@rpetrusha
Copy link

@davidsh, could you take a look at this PR, please?

@davidsh
Copy link
Contributor

davidsh commented Aug 2, 2019

@davidsh, could you take a look at this PR, please?

This doc change is accurately describing the current behavior of .NET.

@rpetrusha rpetrusha added the ✨ 1st-time dotnet-api-docs contributor! Indicates PRs from new contributors to the dotnet-api-docs repository label Aug 2, 2019
Copy link

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @davidsh, for the quick review. And thank you, @dpmm99, for contributing to the dotnet/dotnet-api-docs repo and enhancing the level of detail of the documentation. I'll merge your PR now. The change should be live on docs.microsoft.com in the next day or two.

@rpetrusha rpetrusha merged commit e37a539 into dotnet:master Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Net ✨ 1st-time dotnet-api-docs contributor! Indicates PRs from new contributors to the dotnet-api-docs repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants